package com.whut.www.file_storage;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * Created by clvc on 2017/7/1.
 * 真正的速度是看不见的 !
 * Today is today , we will go !
 */

public class SDCardHelper {

    public static boolean isSDCardMounted() {

        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    ///////////////////////////////////////////////////////////////////////////
    // 獲取SD卡路徑
    ///////////////////////////////////////////////////////////////////////////


    public static String getSDCardBaseDir() {
        if (isSDCardMounted()) {
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return null;
    }


    public static String getSDCardPublicDir(String type) {
        if (isSDCardMounted()) {
            return Environment.getExternalStoragePublicDirectory(type).getAbsolutePath();
        }
        return null;
    }


    public static String getSDCardPrivateCacheDir(Context context) {
        if (isSDCardMounted()) {
            /*context.getCacheDir();
            context.getFilesDir();*/
            return context.getExternalCacheDir().getAbsolutePath();
        }
        return null;
    }


    public static String getSDCardPrivateFilesDir(Context context, String type) {
        if (isSDCardMounted()) {
            return context.getExternalFilesDir(type).getAbsolutePath();
        }
        return null;
    }

    ///////////////////////////////////////////////////////////////////////////
    // 往SD卡中保存文件或圖片
    ///////////////////////////////////////////////////////////////////////////


    public static boolean saveFileToSDCardPublicDir(byte[] data, String type, String filename)
            throws Exception {
        String filePath = getSDCardPublicDir(type) + File.separator + filename;
        if (isSDCardMounted()) {
            BufferedOutputStream bos = null;
            bos = new BufferedOutputStream(new FileOutputStream(new File(filePath)));
            bos.write(data, 0, data.length);
            bos.flush();
            return true;
        }
        return false;
    }


    public static boolean saveFileToSDCardCustomDir(byte[] data, String dirs, String filename)
            throws Exception {
        if (isSDCardMounted()) {
            File file_dir = new File(getSDCardBaseDir() + File.separator + dirs);
            if (!file_dir.exists()) {
                file_dir.mkdirs();
            }
            BufferedOutputStream bos = null;
            bos = new BufferedOutputStream(new FileOutputStream(new File(file_dir, filename)));
            bos.write(data, 0, data.length);
            bos.flush();
            return true;
        }
        return false;
    }


    public static boolean saveFileToSDCardPrivateCacheDir(byte[] data, String filename, Context
            context) throws Exception {
        if (isSDCardMounted()) {
            String filepath = getSDCardPrivateCacheDir(context) + File.separator + filename;
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File
                    (filepath)));
            bos.write(data, 0, data.length);
            bos.flush();
            return true;
        }
        return false;
    }


    public static boolean saveBitmapToSDCardPrivateCacheDir(Bitmap bm, String filename, Context
            context) throws Exception {
        if (isSDCardMounted()) {
            String filepath = getSDCardPrivateCacheDir(context) + File.separator + filename;
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File
                    (filepath)));
            bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
            bos.flush();
            return true;
        }
        return false;
    }


    public static boolean saveBitmapToSDCardPublicDir(Bitmap bm, String type, String filename)
            throws Exception {
        if (isSDCardMounted()) {
            String filepath = getSDCardPublicDir(type) + File.separator + filename;
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File
                    (filepath)));
            bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
            bos.flush();
            return true;
        }
        return false;
    }


    public static boolean saveFileToSDCardPrivateFilesDir(byte[] data, String type, String
            filename, Context context) throws Exception {
        if (isSDCardMounted()) {
            String filepath = getSDCardPrivateFilesDir(context, type) + File.separator + filename;
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File
                    (filepath)));
            bos.write(data, 0, data.length);
            bos.flush();
            return true;
        }
        return false;
    }

    ///////////////////////////////////////////////////////////////////////////
    // 從SD卡中加載文件
    ///////////////////////////////////////////////////////////////////////////
    public static byte[] loadFileFromSDCard(String filePath) throws Exception {
        if (isSDCardMounted()) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File
                    (filePath)));
            byte[] buffer = new byte[1024 * 8];
            int c = 0;
            while ((c = bis.read(buffer)) != -1) {
                baos.write(buffer, 0, c);
                baos.flush();
            }
            return baos.toByteArray();
        }
        return null;
    }

    public static Bitmap loadBitmapFromSDcard(String filePath) throws Exception {
        byte[] data = loadFileFromSDCard(filePath);
        if (data != null) {
            Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            return bm;
        }
        return null;
    }
}
